/*
 * Copyright (C) 2011 Macambira Team
 * 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.googlecode.control;

import com.googlecode.entity.Professor;
import com.googlecode.exception.ProfessorException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Classe responsavel por realizar o CRUD de professor
 * 
 * @version 0.2 22 set 2011
 * @author Italo Cesar
 */
public class ControlProfessor{

    private Map<String, Professor> mapProfessores;
    private List<Professor> listProfessores;

    public ControlProfessor() {
        this.mapProfessores = new HashMap<String, Professor>();
        this.listProfessores = new LinkedList<Professor>();
    }

    public void addProfessor(Professor p) {
        this.mapProfessores.put(p.getId(), p);
        this.listProfessores.add(p);
    }

    public Professor getProfessorID(String id) throws ProfessorException {
        return mapProfessores.get(id);
    }

    public Professor getProfessorNome(String nome) throws ProfessorException{
        Professor aux = null;
        for (Professor p : listProfessores) {
            if (p.getNome().equalsIgnoreCase(nome)) {
                aux = p;
                break;
            }
        }
        return aux;
    }

    public void removeProfessorNome(String nome) throws ProfessorException {
        for (Professor p : listProfessores) {
            if (p.getNome().equalsIgnoreCase(nome)) {
                this.mapProfessores.remove(p.getId());
                break;
            }
        }
        try {
            this.listProfessores.remove(getProfessorNome(nome));
        } catch (ProfessorException ex) {
            Logger.getLogger(ControlProfessor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void removeProfessorID(String id)throws ProfessorException {
        for (Professor p : listProfessores) {
            if (p.getId().equalsIgnoreCase(id)) {
                this.listProfessores.remove(p);
                break;
            }
        }
        this.mapProfessores.remove(id);
    }

    public List<Professor> getListaProfessores() throws ProfessorException {
        return this.listProfessores;
    }

    public Map<String, Professor> getMapProfessores() throws ProfessorException {
        return this.mapProfessores;
    }

    public int listGetSize() {
        return this.listProfessores.size();
    }

    public int mapGetSize() {
        return this.mapProfessores.size();
    }
    
    public Professor getObjetoProfessor(Professor procurado){
        Professor p = null;
        for (Professor p2 : this.listProfessores){
            if (p2.equals(procurado)){
               p = p2;
            }
        }
        return p;
    }
}